package com.tadpole.music.view.activity.me.yun;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.Settings;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.luck.picture.lib.permissions.RxPermissions;
import com.tadpole.music.R;
import com.tadpole.music.bean.me.yun.YunStudentInfoBean;
import com.tadpole.music.iView.me.yun.YunStudentInfoIView;
import com.tadpole.music.presenter.me.yun.YunStudentInfoPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.base.BaseActivity;

import io.reactivex.functions.Consumer;

/**
 * 蝌蚪云考试 考生信息
 * Created By 孙岩
 * on 2021/2/9
 */
public class YunStudentInfoActivity extends BaseActivity implements YunStudentInfoIView {
    //回退
    private View view_back_icon;
    //姓名
    private TextView tvName;
    //身份证号
    private TextView tvCardNumber;
    //考点
    private TextView tv;
    //注意事项
    private TextView tv_content;
    //去考试
    private LinearLayout llGo;
    //倒计时
    private TextView tvTime;
    //倒计时
    private TimeCount time;
    //获取考生信息
    private YunStudentInfoPresenter yunStudentInfoPresenter;
    //数据源
    private YunStudentInfoBean.DataBean bean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yun_student_info);

        time = new TimeCount(5000,1000);
        time.start();

        initViews();
        initListeners();

        yunStudentInfoPresenter = new YunStudentInfoPresenter();
        yunStudentInfoPresenter.attachView(this);
        yunStudentInfoPresenter.getStudentInfo();
    }

    //初始化控件
    private void initViews(){
        view_back_icon = findViewById(R.id.view_back_icon);
        tvName = findViewById(R.id.tvName);
        tvCardNumber = findViewById(R.id.tvCardNumber);
        tv = findViewById(R.id.tv);
        tv_content = findViewById(R.id.tv_content);
        llGo = findViewById(R.id.llGo);
        tvTime = findViewById(R.id.tvTime);
        llGo.setEnabled(false);
    }

    //初始化监听器
    private void initListeners(){
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                YunStudentInfoActivity.this.finish();
            }
        });

        llGo.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
                    if(bean.isIs_test()){
                        ToastUtils.show("已经考过试,请勿重复提交");
                    }else{
                        if(bean.getExam().getType().equals("question")){//音基考试
                            andPermission();
                        }else if(bean.getExam().getType().equals("musical_instrument")){//乐器考试
                            Intent intent = new Intent(YunStudentInfoActivity.this,YunExaminationOneActivity.class);
                            intent.putExtra("type",bean.getExam().getWorks_type());
                            intent.putExtra("title",bean.getExam().getTitle());
                            intent.putExtra("name",bean.getName());
                            intent.putExtra("card",bean.getIdcode());
                            startActivityForResult(intent,1024);
                        }
                    }
                }else{
                    ToastUtils.show(getResources().getString(R.string.net_work));
                }
            }
        });
    }

    //权限
    @SuppressLint("CheckResult")
    private void andPermission() {
        RxPermissions rxPermission = new RxPermissions(YunStudentInfoActivity.this);
        rxPermission
                .request(
                        Manifest.permission.MODIFY_AUDIO_SETTINGS,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                ).subscribe(new Consumer<Boolean>() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                //请求权限成功
                if (aBoolean) {
                    Intent intent = new Intent(YunStudentInfoActivity.this,YunExaminationTwoActivity.class);
                    intent.putExtra("title",bean.getExam().getTitle());
                    startActivityForResult(intent,1024);
                } else {//失败
                    String msg = "请点击【去授权】打开相应权限！";
                    showAlert(msg);
                }
            }
        });
    }

    //授权
    public void showAlert(String msg) {
        new AlertDialog.Builder(YunStudentInfoActivity.this).setMessage(msg).
                setPositiveButton("去授权",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", YunStudentInfoActivity.this.getPackageName(), null);
                                intent.setData(uri);
                                startActivity(intent);
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setCancelable(true).show();
    }

    @Override
    public void showInfo(YunStudentInfoBean.DataBean bean) {
        this.bean = bean;
        tvName.setText(bean.getName());
        tvCardNumber.setText(bean.getIdcode());
        tv.setText(bean.getExam_address());
        tv_content.setText(bean.getExam().getContent());
    }

    //倒计时
    class TimeCount extends CountDownTimer {
        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onTick(long millisUntilFinished) {
            tvTime.setText(millisUntilFinished/1000+"s");
        }

        @Override
        public void onFinish() {
            tvTime.setVisibility(View.GONE);
            llGo.setBackgroundResource(R.drawable.shape_yun_red);
            llGo.setEnabled(true);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1024&&resultCode==1024){
            setResult(1024);
            YunStudentInfoActivity.this.finish();
        }
    }
}
